/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cardgame;

import java.awt.Color;
import javax.swing.border.LineBorder;
import cardgame.game.*;

/**
 *
 * @author Carl
 */
public class PanCardInfo extends javax.swing.JPanel{

    public PanCardInfo(){
        initComponents();
        panManaCost.add(panMana);
        reset();
    }
    private Card card;
    private PanCardInfo_Mana panMana = new PanCardInfo_Mana(20);
    
    public void showCard(Card card){
        this.card = card;
        update();
    }
    
    public void update(){
        reset();
        if(card != null){
            lblName.setText(card.getDescription().getTitle());
            String description = "<html>" + card.getDescription().getDescription() + "<br/><br/>";
            if(card instanceof MonsterCard){
                MonsterCard monsterCard = (MonsterCard) card;
                panMana.setManaAmount(monsterCard.getSummonCost().getMana());
                lblStats.setText(" " + monsterCard.getAttackDamage() + " / " + monsterCard.getCurrentLifepoints() + ((monsterCard.getCurrentLifepoints() != monsterCard.getMaximumLifepoints())?"[" + monsterCard.getMaximumLifepoints() + "]":""));
                if(monsterCard.getSpells().length > 0){
                    description += "<u>Spells:</u>";
                    for(Spell spell : monsterCard.getSpells()){
                        description += "<br/>" + (spell.getCost().isTap()?"⌐ ":"") + spell.getDescription().getTitle() + " (" + getHTML_Mana(spell.getCost().getMana()) + ")";
                    }
                }
            }
            else if(card instanceof SpellCard){
                SpellCard spellCard = (SpellCard) card;
                panMana.setManaAmount(spellCard.getSpell().getCost().getMana());
            }
            if(!(card instanceof SimpleManaCard)){
                lblImage.setBorder(new LineBorder(Color.BLACK));
                lblImage.setBackground(Color.WHITE);
                lblImage.setOpaque(true);
            }
            description += "</html>";
            lblImage.setIcon(Util.getResourceImageIcon(getCardImageResourcePath(card), 100, 100));
            lblDescription.setText(description);
        }
    }
    
    private static String getHTML_Mana(ManaAmount manaAmount){
        String html = "";
        for(int i=0;i<Mana.values().length;i++){
            Mana mana = Mana.values()[i];
            Color manaColor = ManaAmount.getColor(mana);
            if(i != 0){
                html += ", ";
            }
            html += "<b style=\"color:rgb(" + manaColor.getRed() + "," + manaColor.getGreen() + "," + manaColor.getBlue() + ");\">" + manaAmount.getMana(mana) + "</b>";
        }
        return html;
    }
    
    public void reset(){
        lblName.setText("");
        lblImage.setBorder(null);
        lblImage.setBackground(null);
        lblImage.setOpaque(false);
        lblImage.setIcon(null);
        panMana.setManaAmount(null);
        lblStats.setText("");
        lblDescription.setText("");
    }
    
    public static String getCardImageResourcePath(Card card){
        String imageResourcePath = "/cardgame/resources/images/";
        String titleImageSuffix = ("cards/" + card.getDescription().getTitle());
        if(Util.existsResource(imageResourcePath + titleImageSuffix + ".gif")){
            imageResourcePath += titleImageSuffix + ".gif";
        }
        else if(Util.existsResource(imageResourcePath + titleImageSuffix + ".png")){
            imageResourcePath += titleImageSuffix + ".png";
        }
        else if(Util.existsResource(imageResourcePath + titleImageSuffix + ".jpg")){
            imageResourcePath += titleImageSuffix + ".jpg";
        }
        else if(card instanceof SimpleManaCard){
            SimpleManaCard simpleManaCard = (SimpleManaCard) card;
            imageResourcePath += "mana/" + ManaAmount.getIndex(simpleManaCard.getMana()) + ".png";
        }
        else{
            imageResourcePath += "cards/Other.png";
        }
        return imageResourcePath;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        lblImage = new javax.swing.JLabel();
        lblDescription = new javax.swing.JLabel();
        lblStats = new javax.swing.JLabel();
        panManaCost = new javax.swing.JPanel();

        setBackground(new java.awt.Color(153, 153, 153));

        lblName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblName.setText("?");

        lblDescription.setVerticalAlignment(javax.swing.SwingConstants.TOP);

        lblStats.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lblStats.setText("?");

        panManaCost.setOpaque(false);
        panManaCost.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDescription, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblStats, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                            .addComponent(panManaCost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(lblName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblName, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(panManaCost, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblStats))
                    .addComponent(lblImage, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblImage;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblStats;
    private javax.swing.JPanel panManaCost;
    // End of variables declaration//GEN-END:variables
}
